const express = require('express')
const cors = require('cors')

const app = express()

app.use(express.json())
app.use(cors())

app.get('/calculateTax', (request, response) => {
  const { query } = request

  const money = query?.money ?? 0
  let tax = 0

  const needPay = money - 5000

  if (needPay > 0 && needPay <= 3000) {
    tax = needPay * 0.03
  } else if (needPay > 3000 && needPay <= 12000) {
    tax = 3000 * 0.03 + (needPay - 3000) * 0.1
  } else if (needPay > 12000 && needPay <= 25000) {
    tax = 3000 * 0.03 + 9000 * 0.1 + (needPay - 12000) * 0.2
  } else if (needPay > 25000 && needPay <= 35000) {
    tax = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + (needPay - 25000) * 0.25
  } else if (needPay > 35000 && needPay <= 55000) {
    tax =
      3000 * 0.03 +
      9000 * 0.1 +
      13000 * 0.2 +
      10000 * 0.25 +
      (needPay - 35000) * 0.3
  } else if (needPay > 55000 && needPay <= 80000) {
    tax =
      3000 * 0.03 +
      9000 * 0.1 +
      13000 * 0.2 +
      10000 * 0.25 +
      20000 * 0.3 +
      (needPay - 55000) * 0.35
  } else if (needPay > 80000) {
    tax =
      3000 * 0.03 +
      9000 * 0.1 +
      13000 * 0.2 +
      10000 * 0.25 +
      20000 * 0.3 +
      25000 * 0.35 +
      (needPay - 80000) * 0.45
  }

  tax = tax.toFixed(2)

  response.send(`<h1>工资为${money}元所缴纳的税为：${tax}元</h1>`)
})

app.listen(8000, () => {
  console.log('服务器启动于http://localhost:8000')
})
